package com.franklin.validation.tool.utils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Description :
 * Create By: Franklin
 * Date : 2020/6/23 12:12
 */
public abstract class DataCheckUtils {

    public static final String FORMAT_0 = "yyyy-MM-dd HH:mm:ss";

    /**
     * 非法字符串
     */
    private static Set<String> errorStrings;

    static {
        errorStrings = new HashSet<>();
        errorStrings.add("''");
        errorStrings.add("\"\"");
        errorStrings.add("null");
        errorStrings.add("");
    }

    public static boolean isEmpty(Object obj) {
        if (null == obj) {
            return true;
        }
        if ((obj instanceof String)) {
            String trim = ((String) obj).trim();
            return errorStrings.contains(trim);
        }
        if ((obj instanceof Collection)) {
            return ((Collection) obj).size() == 0;
        }
        if ((obj instanceof Map)) {
            return ((Map) obj).size() == 0;
        }
        return false;
    }

    public static boolean isNotEmpty(Object obj){
        return !isEmpty(obj);
    }

    public static String formatDate(String pattern, Date date){
        return new SimpleDateFormat(pattern).format(date);
    }

    public static String defaultDate(){
        return formatDate(FORMAT_0,new Date());
    }

    public static String defaultDate(String pattern){
        return formatDate(pattern,new Date());
    }

    public static boolean isCollection(String paramType){
        if (paramType.contains("<")){
            String[] split = paramType.split("<");
            try {
                Class<?> aClass = Class.forName(split[0]);
                if (Collection.class.isAssignableFrom(aClass)){
                    return true;
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
